/*
 * CS_REQ_SHOP_SELL_ITEM.cpp
 *
 *  Created on: 2013. 5. 27.
 *      Author: the2
 */

#include "../../Command.h"
#include "../../NResource/Static.h"
#include "../../NLogic/Item.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/DailyMission.h"
#include "../../NLogic/VirtualCurrency.h"
#include "../../User.h"
#include "../../GameLog.h"
#include "../../NUtil/Utils.h"

namespace ProjectZ { namespace NCommand {

void SendPacket_CS_REQ_SHOP_SELL_ITEM(User * pUser, int16_t res) {

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	*pAck << res;
	*pAck << pUser->GetCharacterInfo().gold;
	*pAck << pUser->GetVirtualCurrency()->GetZen();

	pAck->MakePacket(CMD_SC_REQ_SHOP_SELL_ITEM);

	pUser->Send(pAck);
}

void Command::CS_REQ_SHOP_SELL_ITEM(User * pUser, ::Network::Packet * pPacket)
{
	PREPARE_PACKET;

	char bagType		= 0;
	uint64_t itemSeq	= 0;
	char itemQuantity	= 0;

	*pPacket >> bagType >> itemSeq >> itemQuantity;

	if (itemQuantity < 0)
	{
		SendPacket_CS_REQ_SHOP_SELL_ITEM(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NLogic::Bag * pBag = pUser->GetBag(static_cast<INVEN_BAG_TYPE>(bagType));

	if (pBag == NULL)
	{
		SendPacket_CS_REQ_SHOP_SELL_ITEM(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NLogic::Item * pItem = pBag->GetItem(itemSeq);

	if (pItem == NULL)
	{
		SendPacket_CS_REQ_SHOP_SELL_ITEM(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	if (true == pItem->IsEquip())
	{
		SendPacket_CS_REQ_SHOP_SELL_ITEM(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	//pItem->GetDate().
	if (0 == NResource::Static::Instance().GetItemResource()->GetItemPrice(pItem->GetData()))
	{
		SendPacket_CS_REQ_SHOP_SELL_ITEM(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	// 아이템 보유 수량 보다 요청 수량이 많은 경우 에러 응답
	if (pItem->GetData().quantity < itemQuantity)
	{
		SendPacket_CS_REQ_SHOP_SELL_ITEM(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	// 판매 수량 만큼 보유 수량 차감
	pItem->GetData().quantity -= itemQuantity;

	int sellPrice = 100;

	// 미식별 아이템은 무조건 100.
	if (0 == pItem->GetData().non_identity)
	{
		// 판매 가격 : 기본가격 / ((아이템레벨 / 상수) + 5)
		sellPrice = NResource::Static::InstancePtr()->GetItemResource()->GetItemPrice(pItem->GetData()) / ( ( pItem->GetData().level / 5) + 5);
		sellPrice = sellPrice * itemQuantity;
		sellPrice = NUtil::AddPercentage(sellPrice, -10);
	}

//	pUser->GetCharacterInfo().gold += sellPrice;
	NLogic::NFunction::ModifyGold(pUser, sellPrice);
	NGameLog::LogGoldInfo(pUser, SELL_STORE, sellPrice);

	// 일일 미션 (장비 / 장신구 만 가능)
	if (true == NResource::Static::Instance().GetItemResource()->isEquip(pItem->GetData()))
	{
		NLogic::DailyMission * pDailyMission = pUser->GetDailyMission();
		if (NULL != pDailyMission)
		{
			pDailyMission->MissionComplete(NLogic::DailyMission::MissionType::MISSION6);
		}
	}

	if (pItem->GetData().quantity <= 0)
	{
		pBag->PopItem(itemSeq);
		pItem->DeleteDatabaseItemInfo(NLogic::Item::ItemStatus::EM_ITEM_STATUS_SELL_STORE);
	}
	else
	{
		pItem->UpdateDatabaseItemInfo();
	}

	SendPacket_CS_REQ_SHOP_SELL_ITEM(pUser, ACK_OK);
}

} /*NCommand*/ } /*ProjectZ*/
